package com.test.ml;

import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Predicate;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
  
/** 
 * <B>文件名称：</B>SwaggerConfig.java<BR> 
 * <B>文件描述：</B><BR> 
 *  
 * <B>版权声明：</B>(C)2014-2015<BR> 
 * <B>公司部门：</B><BR> 
 * <B>创建时间：</B>2016年6月30日<BR> 
 *  http://localhost:8080/swagger-ui.html
 *  http://localhost:8080/dist/index.html
 * @author  
 * @version  
 */  
@Configuration  
//@EnableWebMvc  
@EnableSwagger2  
@ComponentScan({"com.test.ml"})
public class SwaggerConfig {  
  
   // private SpringSwaggerConfig springSwaggerConfig;  
  
      
  
      
//    @Autowired  
//    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {  
//        this.springSwaggerConfig = springSwaggerConfig;  
//    }  
//  
//    /** 
//     * 链式编程 来定制API样式 
//     * 后续会加上分组信息 
//     * 
//     * @return 
//     */  
//    @Bean  
//    public SwaggerSpringMvcPlugin customImplementation() {  
//        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)  
//                .apiInfo(apiInfo())  
//                //.includePatterns(".*")  
//                .apiVersion("0.0.1");  
//        //.swaggerGroup(PROJECT_NAME);  
//    }  
  
    
//    @Bean
//    public Docket configSpringfoxDocketForAll() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .produces(Sets.newHashSet("application/json"))
//                .consumes(Sets.newHashSet("application/json"))
//                .protocols(Sets.newHashSet("http"/*, "https"*/)) 
//                .forCodeGeneration(true)
//                .select()
//                .build()
//                .apiInfo(apiInfo());
//    }
//    private ApiInfo apiInfo() {  
//    	   ApiInfo apiInfo = new ApiInfo(
//                   "API 文档",
//                   "接口文档平台",
//                   "1.0.0",
//                   "http://localhost:80/demo/v2/api-docs",
//                   new Contact("li", "", "li@163.com"),
//                   "开源协议",
//                   "http://www.apache.org/licenses/LICENSE-2.0.html"
//           );
//           return apiInfo;
//    }  
	
    @Bean
    public Docket createRestApi() {
        Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler input) {
                Class<?> declaringClass = input.declaringClass();
                if (declaringClass == BasicErrorController.class)// 排除
                    return false;
                if(declaringClass.isAnnotationPresent(RestController.class)) // 被注解的类
                    return true;
                if(declaringClass.isAnnotationPresent(Controller.class)) // 被注解的类
                    return true;
                if(input.isAnnotatedWith(ResponseBody.class)) // 被注解的方法
                    return true;
                return false;
            }
        };
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .select()
                .apis(predicate)
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("接口文档 ")//大标题
            .version("1.0")//版本
            .termsOfServiceUrl("http://localhost:8080/v2/api-docs")
            .build();
    }
}  
